$(function () {
    load(null,null);
});

function ConvertJSONDateToJSDateObject(JSONDateString) {

    var date = new Date(JSONDateString);
    var year = date.getFullYear();
    var month = date.getMonth + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
    var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    // var hour = date.getHours();
    // var minute = date.getMinutes();
    // var second = date.getSeconds();
    var datastr = year + "-" + month + "-" + currentDate;
    return datastr;
}
function sousuo(){
    var sname=$("#sousuoa").val();
    load(null,null,sname);
}

function load(currentPage,type,sname){
    sname=$("#sousuoa").val();
    // <li>
    // <a href="detail.jsp" class="ccsl">字画装裱需要注意什么细节问题?</a>
    // <span>2018-09-04</span>
    // </li>
    $.get("paging/news",{currentPageStr:currentPage,type:type,sname:sname},function (News){

        if(type!=null){
            var bt="";
            if(type==0){
                $("#lotbt1").addClass("on");
                $("#lotbt2").removeClass();
            }
            if(type==1){
                $("#lotbt1").removeClass();
                $("#lotbt2").addClass("on");
            }
        }
        var lis = "<ul>";
        // var birthdayMilliseconds = parseInt(obj.Birthday.replace(/\D/igm, ""));
        // //实例化一个新的日期格式，使用1970 年 1 月 1 日至今的毫秒数为参数
        // var birthday = new Date(birthdayMilliseconds);

        /*<li className="wow slideInUp" data-wow-delay=".2s">
            <div className="border_top"></div>
            <div className="border_right"></div>
            <div className="border_bottom"></div>
            <div className="border_left"></div>
            <a href="/a/hyxw/70.html" className="news-more mt15">
                <h5 className="pb5 pt15 f16 a66">英格兰式与荷兰式相结合的拍卖方式</h5>
                <span className="f12 block y ar"><strong>03-13</strong>2018</span>
                <p className="line22 a99 pt5">也称降价拍卖或高估价拍卖。是指在拍卖过程中，拍卖人宣布拍卖标的的起拍价及降幅，并依次叫价，第一位应价人响槌成交。但成交价不得低于保留价。英格兰式与荷兰式相结合的拍...</p>
            </a></li>
            <li class="wow slideInUp animated animated" data-wow-delay=".2s" style="visibility: visible; animation-delay: 0.2s;">
              <div class="border_top"></div>
              <div class="border_right"></div>
              <div class="border_bottom"></div>
              <div class="border_left"></div>
              <a href="/a/hyxw/70.html" class="news-more mt15">
          <h5 class="pb5 pt15 f16 a66">英格兰式与荷兰式相结合的拍卖方式</h5>
          <span class="f12 block y ar"><strong>03-13</strong>2018</span>
          <p class="line22 a99 pt5">也称降价拍卖或高估价拍卖。是指在拍卖过程中，拍卖人宣布拍卖标的的起拍价及降幅，并依次叫价，第一位应价人响槌成交。但成交价不得低于保留价。英格兰式与荷兰式相结合的拍...</p>
          </a></li>
          <li class="wow slideInUp animated animated" data-wow-delay=".2s" style="visibility: visible; animation-delay: 0.2s;">

            */

        for (var i = 0; i < News.list.length; i++) {
            var route = News.list[i];
            var date = ConvertJSONDateToJSDateObject(route.pubdate);
            var date1=new Date(route.pubdate);
            var li = '<li class="wow slideInUp" data-wow-delay="'+(i+2)+'s" style="visibility: visible; animation-delay: '+(i+2)+'s;">\n'+
                    '<div class="border_top"></div>\n'+
                    '<div class="border_right"></div>\n'+
                    '<div class="border_bottom"></div>\n'+
                    '<div class="border_left"></div>\n'+
                    '<a href="jump/detail?id='+route.id+'" class="news-more mt15">'+
                    '<h5 class="pb5 pt15 f16 a66">'+route.title+'</h5>\n'+
                    '<span class="f12 block y ar"><strong>'+date1.getMonth()+'-'+date1.getDay()+'</strong>'+date1.getFullYear()+'</span>\n'+
                    '<p class="line22 a99 pt5">'+route.intrduce+'</p>\n'+
                    '</a></li>';


            // var li = '<li>\n'+
            //     //href="javascript:void(0)" onclick="Clickadd()"
            //     '<a href="javascript:void(0)" onclick="Clickadd('+route.id+')" class="ccsl">'+route.title+'</a>\n'+
            //     '<span>'+date+'</span>\n'+
            //     '</li>';
            lis+=li;
        }
        lis+='</ul>'

        $("#listul").html(lis);

        window.scroll(0,0);


        var liss = "";
        var fristPage = '<li onclick="javascript:load('+1+',\''+type+'\',\''+sname+'\')"><a href="javascript:void(0)">首页</a></li>'

        var beforeNum = News.currentPage - 1;
        if(beforeNum<=0){
            beforeNum=1;
        }
        var beforePage = '<li class="threeword" onclick="javascript:load('+beforeNum+',\''+type+'\',\''+sname+'\')"><a href="javascript:void(0)">上一页</a></li>';
        liss += fristPage;
        liss += beforePage;

        // 定义开始位置begin,结束位置 end
        var begin; // 开始位置
        var end ; //  结束位置


        //1.要显示10个页码
        if(News.totalPage < 10){
            //总页码不够10页
            begin = 1;
            end = News.totalPage;
        }else{
            //总页码超过10页

            begin = News.currentPage - 5 ;
            end = News.currentPage + 4 ;

            //2.如果前边不够5个，后边补齐10个
            if(begin < 1){
                begin = 1;
                end = begin + 9;
            }
            //3.如果后边不足4个，前边补齐10个
            if(end > News.totalPage){
                end = News.totalPage;
                begin = end - 9 ;
            }
        }

        for(var i=begin; i<=end;i++){
            var li;
            if(News.currentPage==i){
                li = '<li class="curPage" onclick="javascript:load('+i+',\''+type+'\',\''+sname+'\')"><a href="javascript:void(0)">'+i+'</a></li>'
            }else {
                li = '<li onclick="javascript:load('+i+',\''+type+'\',\''+sname+'\')"><a href="javascript:void(0)">'+i+'</a></li>'
            }

            liss += li;
        }


        var lastPage = '<li class="threeword" onclick="javascript:load('+News.totalPage+',\''+type+'\',\''+sname+'\')"><a href="javascript:void(0)">末页</a></li>';
        var afterNum = News.currentPage+1;
        if(afterNum>=News.totalPage){
            afterNum =News.totalPage;
        }
        var nextPage = '<li class="threeword" onclick="javascript:load('+afterNum+',\''+type+'\',\''+sname+'\')"><a href="javascript:void(0)">下一页</a></li>';

        liss += nextPage;
        liss += lastPage;

        //将lis内容设置到 ul
        $("#pageNum").html(liss);
    });
}